/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source
 * License version 1.1, a copy of which has been included with this
 * distribution in the License.txt file.
 */

package jdbcmanager.dbtools.tableditor;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

import jdbcmanager.awt.*;

/**
 * A JPanel that contains a child of JxTableParent and buttons to add, delete and shift rows. 
 *  
 * @author Francisco Morero Peyrona
 */
final class JPanelParent extends JPanel
{
   private JButton btnAddRow       = new JButton();
   private JButton btnDelRow       = new JButton();
   private JButton btnShiftUpRow   = new JButton();
   private JButton btnShiftDownRow = new JButton();
   
   private JxTableParent table;
   
   //-------------------------------------------------------------------------//
   
   JPanelParent( JxTableParent table )
   {
      this.table = table;
      
      init();
      refreshButtons();
   }
   
   //-------------------------------------------------------------------------//   
   
   private void onAddRow()
   {
      this.table.addRow();
      Rectangle rect = this.table.getCellRect( this.table.getRowCount() -1, 0, true );
      ((JViewport) this.table.getParent()).scrollRectToVisible( rect );    // Scrolls to last row
      refreshButtons();
   }
   
   private void onDelRow()
   {
      this.table.delRow();
      refreshButtons();
   }
   
   private void onShiftUpRow()
   {
      this.table.shiftUpRow();
      refreshButtons();
   }
   
   private void onShiftDownRow()
   {
      this.table.shiftDownRow();
      refreshButtons();
   }
   
   private void refreshButtons()
   {
      int nRows = this.table.getRowCount();
      int nPos  = this.table.getSelectedRow();
      
      this.btnDelRow.setEnabled( nRows > 0 );
      this.btnShiftDownRow.setEnabled( (nRows > 0) && (nPos < nRows - 1) );
      this.btnShiftUpRow.setEnabled( (nRows > 0) && (nPos > 0) );
   }
   
   private void init()
   {
      // Add listener to table ---------------------------
      this.table.getSelectionModel().addListSelectionListener( new ListSelectionListener()
      {
         public void valueChanged( ListSelectionEvent evt )
         {
            if( ! evt.getValueIsAdjusting() )
               refreshButtons();
         }
      } );
      
      // Panel East ------------------------------------------------
      btnAddRow.setIcon( new ImageIcon( getClass().getResource( "images/row_insert.gif" ) ) );
      btnAddRow.setToolTipText( "Add new key row" );
      btnAddRow.setPreferredSize( new Dimension( 28,28 ) );
      btnAddRow.setMaximumSize( new Dimension( 28,28 ) );
      btnAddRow.addActionListener( new ActionListener()
      {
         public void actionPerformed( ActionEvent evt ) { onAddRow(); } 
      } );

      btnDelRow.setIcon( new ImageIcon( getClass().getResource( "images/row_delete.gif" ) ) );
      btnDelRow.setToolTipText( "Delete highlighted key row" );
      btnDelRow.setPreferredSize( new Dimension( 28,28 ) );
      btnDelRow.setMaximumSize( new Dimension( 28,28 ) );
      btnDelRow.addActionListener( new ActionListener()
      {
         public void actionPerformed( ActionEvent evt )  { onDelRow(); }
      } );
      
      JButton btnSeparator = new JButton();
              btnSeparator.setEnabled( false );
              btnSeparator.setOpaque( false );
              btnSeparator.setRequestFocusEnabled( false );
              btnSeparator.setBorderPainted( false );
              btnSeparator.setPreferredSize( new Dimension( 28,28 ) );
              btnSeparator.setMaximumSize( new Dimension( 28,28 ) );
     
      btnShiftUpRow.setIcon( new ImageIcon( getClass().getResource( "images/row_up.gif" ) ) );
      btnShiftUpRow.setToolTipText( "Shift up highlighted row" );
      btnShiftUpRow.setPreferredSize( new Dimension( 28,28 ) );
      btnShiftUpRow.setMaximumSize( new Dimension( 28,28 ) );
      btnShiftUpRow.addActionListener( new ActionListener()
      {
         public void actionPerformed( ActionEvent evt )  { onShiftUpRow(); }
      } );


      btnShiftDownRow.setIcon( new ImageIcon( getClass().getResource( "images/row_down.gif" ) ) );
      btnShiftDownRow.setToolTipText( "Shift down highlighted row" );
      btnShiftDownRow.setPreferredSize( new Dimension( 28,28 ) );
      btnShiftDownRow.setMaximumSize( new Dimension( 28,28 ) );
      btnShiftDownRow.addActionListener( new ActionListener()
      {
          public void actionPerformed( ActionEvent evt )  { onShiftDownRow(); }
      } );

      JPanel pnlEast = new JPanel( new VerticalFlowLayout() );
             pnlEast.add( btnAddRow );
             pnlEast.add( btnDelRow );
             pnlEast.add( btnSeparator );
             pnlEast.add( btnShiftUpRow );
             pnlEast.add( btnShiftDownRow );
      
      // Table's ScrollPane -----------------------------------------
      JScrollPane spTable = new JScrollPane( this.table );
                  spTable.setBorder( BorderFactory.createLineBorder( Color.darkGray, 1 ) );
                              
      // Table + its Buttons ----------------------------------------
      setLayout( new BorderLayout() );
      setBorder( BorderFactory.createEmptyBorder( 8,8,8,8 ) );
      add( spTable, BorderLayout.CENTER );
      add( pnlEast, BorderLayout.EAST   );
   }
}